/* TotalDcmListerAboutView.cpp -- Source file for class TotalDcmListerAboutView
*
* TotalDcmLister - A lister plugin for Total Commander for listing DICOM File content.
*
* Copyright (C) 2015 Captain Stark
*
* This software may be modified and distributed under the terms of the MIT license.
* See the COPYRIGHT file at the top-level directory of this distribution for details.
*/

#include "stdafx.h"
#include "TotalDcmListerAboutView.h"
#include "afxdialogex.h"

#include <string>
#include <sstream>
#include <vector>

using namespace std;

IMPLEMENT_DYNAMIC(TotalDcmListerAboutView, CDialogEx)

/// <summary>
/// Initializes a new instance of the <see cref="TotalDcmListerAboutView"/> class.
/// </summary>
/// <param name="pParent">The p parent.</param>
TotalDcmListerAboutView::TotalDcmListerAboutView(CWnd* pParent /*=NULL*/)
   : CDialogEx(TotalDcmListerAboutView::IDD, pParent)
{

}

/// <summary>
/// Finalizes an instance of the <see cref="TotalDcmListerAboutView"/> class.
/// </summary>
TotalDcmListerAboutView::~TotalDcmListerAboutView()
{
}

/// <summary>
/// Does the dialog data exchange.
/// </summary>
/// <param name="pDX">A pointer to a CDataExchange object.</param>
void TotalDcmListerAboutView::DoDataExchange(CDataExchange* pDX)
{
   CDialogEx::DoDataExchange(pDX);
   DDX_Control(pDX, IDC_STATICABOUTCAPTION, m_ctrlCaption);
   DDX_Control(pDX, IDC_STATICPARAGRAPH1, m_ctrlParagraph1);
   DDX_Control(pDX, IDC_STATICABOUTPARA2, m_ctrlParagraph2);
}

BEGIN_MESSAGE_MAP(TotalDcmListerAboutView, CDialogEx)
   ON_NOTIFY(NM_CLICK, IDC_LINKTOHOMEPAGE, &TotalDcmListerAboutView::OnClickLinkToHomepage)
END_MESSAGE_MAP()

/// <summary>
/// Called in response to the WM_INITDIALOG message. Initializes all controls.
/// </summary>
/// <returns>
/// True, when input focus is set to one of the controls in the dialog box. False otherwise.
/// </returns>
BOOL TotalDcmListerAboutView::OnInitDialog()
{
   CDialogEx::OnInitDialog();

   m_fontCaption.CreatePointFont(120, L"Segoe UI");
   m_ctrlCaption.SetFont(&m_fontCaption);

   wstringstream caption;
   caption << L"Total DICOM Lister - A lister plugin for Total Commander";

   wstring version = GetVersionInfo();
   if (false == version.empty())
   {
      caption << L" (" << version << L")";
   }
   
   m_ctrlCaption.SetWindowTextW(caption.str().c_str());

   CString para1;
   para1.LoadStringW(IDS_STRING_ABOUT_PARA1);
   para1.Replace(L"\n", L"\n");
   m_ctrlParagraph1.SetWindowTextW(para1);

   CString para2;
   para2.LoadStringW(IDS_STRING_ABOUT_PARA2);
   para2.Replace(L"\n", L"\n");
   m_ctrlParagraph2.SetWindowTextW(para2);

   return TRUE;
}

/// <summary>
/// Gets the version information from the library assembly.
/// </summary>
/// <returns>
/// The version string.
/// </returns>
wstring TotalDcmListerAboutView::GetVersionInfo()
{
   HINSTANCE instance = AfxGetInstanceHandle();

   TCHAR fileName[MAX_PATH];
   GetModuleFileName(instance, fileName, sizeof(fileName));

   DWORD  versionHandle = NULL;
   DWORD  versionSize = GetFileVersionInfoSize(fileName, &versionHandle);

   if (versionSize != NULL)
   {
      LPSTR versionBlock = new char[versionSize];

      if (GetFileVersionInfo(fileName, versionHandle, versionSize, versionBlock))
      {
         UINT   versionBlockSize = 0;
         LPBYTE versionBlockData = NULL;

         if (VerQueryValue(versionBlock, L"\\", (VOID FAR* FAR*)&versionBlockData, &versionBlockSize))
         {
            if (versionBlockSize > 0)
            {
               VS_FIXEDFILEINFO* versionInfo = (VS_FIXEDFILEINFO*)versionBlockData;
               if (versionInfo->dwSignature == 0xfeef04bd)
               {
                  wstringstream versionBuilder;

                  versionBuilder 
                     << HIWORD(versionInfo->dwProductVersionMS) << L"."
                     << LOWORD(versionInfo->dwProductVersionMS) << L"."
                     << HIWORD(versionInfo->dwProductVersionLS);

                  return versionBuilder.str();
               }
            }
         }
      }
   }

   return L"";
}

/// <summary>
/// Called when the homepage hyperlink was clicked.
/// </summary>
/// <param name="pNMHDR">
/// A pointer to an NMHDR structure that contains the notification code and additional information.
/// </param>
/// <param name="pResult">The result code.</param>
void TotalDcmListerAboutView::OnClickLinkToHomepage(NMHDR *pNMHDR, LRESULT *pResult)
{
   PNMLINK pNMLink = (PNMLINK)pNMHDR;

   ShellExecuteW(NULL, L"open", pNMLink->item.szUrl, NULL, NULL, SW_SHOWNORMAL);

   this->OnCancel();
   
   *pResult = 0;
}
